package TwoPointers;

import java.util.Arrays;

public class _881_BoatstoSavePeople {
    //each boat at most 2 people
    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int count = 0;
        int start = 0;
        for (int i = people.length - 1; i >= start; i--) {
            if (people[i] >= limit) {
                count++;
            } else {
                int weight = people[i];
                if (start<= i && people[start] + weight <= limit) {
                    start++;
                }
                count++;
            }
        }
        return count;
    }
}
